package com.cq.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期Util类
 */
public class DateUtil {
	 private static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss";

	    /**
	     * 获得默认的 date pattern
	     */
	    public static String getDatePattern() {
	        return defaultDatePattern;
	    }

	    /**
	     * 返回预设Format的当前日期字符串
	     */
	    public static String getToday() {
	        Date today = new Date();
	        return format(today);
	    }

	    /**
	     * 使用预设Format格式化Date成字符串
	     */
	    public static String format(Date date) {
	        return format(date, getDatePattern());
	    }

	    /**
	     * 使用参数Format格式化Date成字符串
	     */
	    public static String format(Date date, String pattern) {
	        String returnValue = "";

	        if (date != null) {
	            SimpleDateFormat df = new SimpleDateFormat(pattern);
	            returnValue = df.format(date);
	        }

	        return (returnValue);
	    }

	    /**
	     * 使用预设格式将字符串转为Date
	     */
	    public static Date parse(String strDate) throws ParseException {
	        return parse(strDate, getDatePattern());
	    }

	    /**
	     * 使用参数Format将字符串转为Date
	     */
	    public static Date parse(String strDate, String pattern) throws ParseException {
	        SimpleDateFormat df = new SimpleDateFormat(pattern);
	        return df.parse(strDate);
	    }

	    /**
	     * 在日期上增加数个整月
	     */
	    public static Date addMonth(Date date, int n) {
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.add(Calendar.MONTH, n);
	        return cal.getTime();
	    }
	    
	    /**
	     * 在日期上加多少天
	     * @param date
	     * @param n
	     * @return
	     */
	    public static Date addDay(Date date, int n) {
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.add(Calendar.DAY_OF_WEEK, n);
	        return cal.getTime();
	    }
	    /**
	     * 在日期上增加数个整分钟
	     */
	    public static String addMinute(Date date,int n){
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.add(Calendar.MINUTE, n);       
	        return format(cal.getTime());
	    }
	    /**
	     * 在日期上增加数个整秒
	     */
	    public static String addSecond(Date date,int n){
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.add(Calendar.SECOND, n);       
	        return format(cal.getTime());
	    }
	    
//	    public static void main(String[] args){
//	    	Date date=new Date();
//	    	System.out.println(format(date));
//	    	System.out.println(format(addDay(date,-10)));
//	    	System.out.println(format(addDay(date,10)));
//	    }
}